<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属机房" prop="roomId">
        <el-select
          v-model="queryParams.roomId"
          placeholder="请选择所属机房"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in roomList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
	  <el-form-item label="机柜名称" prop="cabinetName">
        <el-input
          v-model="queryParams.cabinetName"
          placeholder="请输入机柜名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
	  <el-form-item label="机柜类型" prop="cabinetType">
	  	<el-select
	  	   v-model="queryParams.cabinetType"
	  	   placeholder="请选择机柜类型"
	  	   clearable
	  	   style="width: 240px">
	  	   <el-option
	  		  v-for="dict in cabinet_type"
	  		  :key="dict.value"
	  		  :label="dict.label"
	  		  :value="dict.value"
	  	   />
	  	</el-select>
	  </el-form-item>
      <el-form-item label="机柜编号" prop="cabinetNo">
        <el-input
          v-model="queryParams.cabinetNo"
          placeholder="请输入机柜编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机柜位置" prop="cabinetPosition">
        <el-input
          v-model="queryParams.cabinetPosition"
          placeholder="请输入机柜位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
	  <el-form-item label="当前状态" prop="status">
	  	<el-select
	  	   v-model="queryParams.status"
	  	   placeholder="请选择当前状态"
	  	   clearable
	  	   style="width: 240px">
	  	   <el-option
	  		  v-for="dict in sys_normal_disable"
	  		  :key="dict.value"
	  		  :label="dict.label"
	  		  :value="dict.value"
	  	   />
	  	</el-select>
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['property:cabinet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['property:cabinet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['property:cabinet:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['property:cabinet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cabinetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属机房" align="center" prop="roomName" />
      <el-table-column label="机柜名称" align="center" prop="cabinetName" />
      <el-table-column label="机柜类型" align="center" prop="cabinetType">
		  <template #default="scope">
		     <dict-tag :options="cabinet_type" :value="scope.row.cabinetType" />
		  </template>
	  </el-table-column>
      <el-table-column label="机柜编号" align="center" prop="cabinetNo" />
      <el-table-column label="机柜品牌" align="center" prop="cabinetBrand" />
      <el-table-column label="机柜位置" align="center" prop="cabinetPosition" />
      <el-table-column label="容量" align="center" prop="capacity" />
      <el-table-column label="已用容量" align="center" prop="useCapacity" />
      <el-table-column label="未用容量" align="center" prop="unuseCapacity" />
      <el-table-column label="状态" align="center" prop="status">
		  <template #default="scope">
		     <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
		  </template>
	  </el-table-column>
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column label="更新时间" align="center" prop="updatedDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="130" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['property:cabinet:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['property:cabinet:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 添加或修改机柜信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="cabinetRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属机房" prop="roomId">
          <el-select
            v-model="form.roomId"
            placeholder="请选择所属机房"
            clearable
          >
            <el-option
              v-for="dict in roomList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
		<el-form-item label="机柜名称" prop="cabinetName">
          <el-input v-model="form.cabinetName" placeholder="请输入机柜名称" />
        </el-form-item>		
		<el-form-item label="机柜类型" prop="cabinetType">
			<el-select
			   v-model="form.cabinetType"
			   placeholder="请选择机柜类型"
			   clearable>
			   <el-option
				  v-for="dict in cabinet_type"
				  :key="dict.value"
				  :label="dict.label"
				  :value="dict.value"
			   />
			</el-select>
		</el-form-item>
        <el-form-item label="机柜编号" prop="cabinetNo">
          <el-input v-model="form.cabinetNo" placeholder="请输入机柜编号" />
        </el-form-item>
        <el-form-item label="机柜品牌" prop="cabinetBrand">
          <el-input v-model="form.cabinetBrand" placeholder="请输入机柜品牌" />
        </el-form-item>
        <el-form-item label="机柜位置" prop="cabinetPosition">
          <el-input v-model="form.cabinetPosition" placeholder="请输入机柜位置" />
        </el-form-item>
        <el-form-item label="容量" prop="capacity">
          <el-input type="number" v-model="form.capacity" placeholder="请输入容量" />
        </el-form-item>
        <el-form-item label="序列号" prop="cabinetSerialNo">
          <el-input v-model="form.cabinetSerialNo" placeholder="请输入机柜序列号" />
        </el-form-item>
        <el-form-item label="机柜规格" prop="cabinetSpec">
          <el-input v-model="form.cabinetSpec" placeholder="请输入机柜规格" />
        </el-form-item>
		<el-form-item label="当前状态" prop="status">
			<el-select
			   v-model="form.status"
			   placeholder="请选择当前状态"
			   clearable
			   style="width: 240px">
			   <el-option
				  v-for="dict in sys_normal_disable"
				  :key="dict.value"
				  :label="dict.label"
				  :value="dict.value"
			   />
			</el-select>
		</el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cabinet">
import { allRoom } from "@/api/property/room";
import { listCabinet,getCabinet,addCabinet,updateCabinet,delCabinet } from "@/api/property/cabinet";
import { ref, reactive, toRefs } from "vue";

const { proxy } = getCurrentInstance()
const { cabinet_type,sys_normal_disable } = proxy.useDict("cabinet_type","sys_normal_disable")

const cabinetList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const roomList = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cabinetName: null,
    cabinetType: null,
    roomId: null,
    cabinetNo: null,
    cabinetBrand: null,
    cabinetPosition: null,
    capacity: null,
    useCapacity: null,
    unuseCapacity: null,
    status: null,
    cabinetSerialNo: null,
    cabinetSpec: null,
    createdBy: null,
    createdDate: null,
    updatedBy: null,
    updatedDate: null
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询机柜信息列表 */
function getList() {
  loading.value = true;
  listCabinet(queryParams.value).then(response => {
    cabinetList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getAllRoomList() {
  loading.value = true;
  allRoom(queryParams.value).then(response => {
    let { code, data } = response;
    if (code === 200) {
      roomList.value = data.map(item => ({ label: item.roomName, value: item.roomId }));
    }
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
	roomId: null,
    cabinetId: null,
    cabinetName: null,
    cabinetType: null,
    cabinetNo: null,
    cabinetBrand: null,
    cabinetPosition: null,
    capacity: null,
    useCapacity: null,
    unuseCapacity: null,
    status: '0',
    cabinetSerialNo: null,
    cabinetSpec: null,
    createdBy: null,
    createdDate: null,
    updatedBy: null,
    updatedDate: null
  };
  proxy.resetForm("cabinetRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.cabinetId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

// 新增按钮操作
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加机柜信息";
}

// 修改按钮操作
function handleUpdate(row) {
  reset();
  const _cabinetId = row.cabinetId || ids.value;
  getCabinet(_cabinetId).then(response => {
	let editData = response.data
	editData.cabinetType = editData.cabinetType+''
	editData.status = editData.status+''
	form.value = editData
    open.value = true;
    title.value = "修改机柜信息";
  });
}

// 提交按钮
function submitForm() {
  proxy.$refs["cabinetRef"].validate(valid => {
    if (valid) {
      if (form.value.cabinetId != null) {
        updateCabinet(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCabinet(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// 删除按钮操作
function handleDelete(row) {
  const _cabinetIds = row.cabinetId || ids.value;
  proxy.$modal.confirm(`是否确认删除机柜信息编号为"${_cabinetIds}"的数据项？`).then(function() {
    return delCabinet(_cabinetIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

// 导出按钮操作
function handleExport() {
  proxy.download('property/cabinet/export', {
    ...queryParams.value
  }, `cabinet_${new Date().getTime()}.xlsx`);
}

getAllRoomList();
getList();
</script>

<style scoped>
/* 你的样式代码 */
</style>
